import 'package:flutter_app/manager/AppSetInfoManager.dart';
import 'package:flutter_app/manager/UserInfoManager.dart';
import 'package:flutter_app/network/Api.dart';
import 'package:flutter_app/network/ResultData.dart';
import 'package:flutter_app/ui/main/main_logic.dart';
import 'package:flutter_app/ui/mine/password_change/password_change_view.dart';
import 'package:flutter_app/ui/mine/suggestion/suggestion_view.dart';
import 'package:flutter_app/utils/PackageInfoUtils.dart';
import 'package:flutter_app/utils/device_info_utils.dart';
import 'package:flutter_app/utils/image_file_info_utils.dart';
import 'package:flutter_app/utils/path_provider_utils.dart';
import 'package:flutter_app/utils/sp_utils.dart';
import 'package:flutter_app/utils/toast_utils.dart';
import 'package:flutter_app/values/CommonParam.dart';
import 'package:get/get.dart';
import 'package:umeng_push_sdk/umeng_push.dart';

import '../../WebViewPage.dart';
import 'set_state.dart';

class SetLogic extends GetxController {
  final SetState state = SetState();
  final mainLogic = Get.find<MainLogic>();

  @override
  void onInit() {
    super.onInit();
  }

  @override
  void onReady() {
    super.onReady();
    initData();
  }

  void initData() {
    getPushState();
    getCacheData();
    getVersion();
    getPinPutState();
  }

  /// 设备确认授权
  Future<void> authDevice(String code) async {
    String deviceId = await DeviceInfoUtils().getDeviceId ?? "";
    String deviceName = await DeviceInfoUtils().getDeviceName ?? "";
    String deviceSys = await DeviceInfoUtils().getDeviceSys ?? "";
    if (deviceId == "") {
      return toast.show("无法获取设备id");
    }

    Map<String, dynamic> param = {
      "deviceId": deviceId,
      "deviceName": deviceName,
      "deviceNum": deviceSys,
      "key": code
    };
    ResultData resultData = await Api.authDevice(param);
    if (resultData.isSuccess) {
      setPinPutState();
      Get.back();
    } else {
      state.pinError.value = resultData.data["message"];
      update();
      toast.show(resultData.data["message"]);
    }
  }

  // 设置 授权状态
  void setPinPutState() {
    // 全局 状态
    mainLogic.state.pinPutState.value = true;
    // 当前 状态
    state.pinState.value = true;
    sp.setBool(CommonParam.PIN_PUT_STATE, true);
    update();
  }

  /// 初始化授权状态
  void getPinPutState() {
    state.pinState.value = mainLogic.state.pinPutState.value;
    update();
  }

  /// 初始化 消息推送状态
  void getPushState() {
    sp.getBool(CommonParam.UMENG_PUSH_ENABLED).then((value) {
      state.push = value ?? true;
      update();
    });
  }

  /// 获取缓存
  void getCacheData() {
    PathProviderUtils().loadCache().then((value) {
      state.dataNum = value;
      update();
    });
  }

  /// 获取当前版本号
  void getVersion() {
    PackageInfoUtils().getVersion().then((value) {
      state.version = value;
      update();
    });
  }

  /// 清除缓存
  void clearAllCache() {
    PathProviderUtils().clearAllCache().then((value) {
      getCacheData();
    });
    ImageFileInfoUtils().removeAll();
  }

  /// 修改推送状态
  void changePushState(bool b) {
    sp.setBool(CommonParam.UMENG_PUSH_ENABLED, b);
    UmengPushSdk.setPushEnable(b);
  }

  /// 跳转 用户协议和隐私政策
  void goToAgreementWebViewPage() {
    Get.to(() => WebViewPage(title: '用户协议和隐私政策', url: AppSetInfoManager().getAgreementUrl(),));
  }

  /// 跳转 应用权限说明
  void goToPermissionWebViewPage() {
    Get.to(() => WebViewPage(title: '应用权限说明', url: AppSetInfoManager().getPermissionUrl(),));
  }

  /// 跳转 修改密码
  void goToPasswordChangePage() {
    Get.to(() => PasswordChangePage(title: "修改密码",));
  }

  /// 意见反馈
  void goToSuggestionPage() {
    Get.to(() => SuggestionPage(title: "意见反馈",));
  }

  /// 退出登录
  void loginOut() {
    UserInfoManager().loginOut();
  }

  @override
  void onClose() {
    super.onClose();
  }
}
